<template>
  <div class="table-panel">
    <div class="table-header">
      <div class="table-info">
        <span class="table-count">共找到 {{ total }} 条预约记录</span>
      </div>
      <div class="table-actions">
        <button class="export-btn" @click="$emit('export')" :disabled="exportLoading">
          <span v-if="!exportLoading" class="btn-content">
            <span class="btn-icon">📄</span>
            导出数据
          </span>
          <span v-else class="btn-loading">
            <span class="loading-spinner"></span>
            导出中...
          </span>
        </button>
      </div>
    </div>
    
    <div class="table-container">
      <el-table 
        :data="bookings" 
        style="width: 100%"
        :loading="tableLoading"
        empty-text="暂无预约数据"
        stripe
        border
        height="500"
      >
        <el-table-column prop="bookingNumber" label="预约编号" width="160" fixed="left">
          <template #default="scope">
            <span class="booking-number">{{ scope.row.bookingNumber }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="contactName" label="联系人" width="100">
          <template #default="scope">
            <span class="contact-name">{{ scope.row.contactName }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="contactPhone" label="联系电话" width="130">
          <template #default="scope">
            <span class="contact-phone">{{ scope.row.contactPhone }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="gender" label="性别" width="80">
          <template #default="scope">
            <span class="personal-info">{{ scope.row.gender || '未填写' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="age" label="年龄" width="80">
          <template #default="scope">
            <span class="personal-info">{{ scope.row.age ? scope.row.age + '岁' : '未填写' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="education" label="教育程度" width="100">
          <template #default="scope">
            <span class="personal-info">{{ scope.row.education || '未填写' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="occupation" label="职业" width="120">
          <template #default="scope">
            <span class="personal-info">{{ scope.row.occupation || '未填写' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="bookingDate" label="预约日期" width="110">
          <template #default="scope">
            <span class="booking-date">{{ formatDate(scope.row.bookingDate) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="periodName" label="场次" width="90">
          <template #default="scope">
            <el-tag 
              :type="getPeriodTagType(scope.row.periodName)"
              size="small"
            >
              {{ scope.row.periodName }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="bookingTime" label="预约时间" width="150">
          <template #default="scope">
            <span class="booking-time">
              {{ scope.row.bookingTimeStart }} - {{ scope.row.bookingTimeEnd }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column prop="status" label="状态" width="90">
          <template #default="scope">
            <el-tag 
              :type="getStatusTagType(scope.row.status)"
              size="small"
            >
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="createdAt" label="预约时间" width="150">
          <template #default="scope">
            <span class="created-time">{{ formatDateTime(scope.row.createdAt) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="notes" label="备注" min-width="150">
          <template #default="scope">
            <span class="notes">{{ scope.row.notes || '-' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button 
              size="small" 
              type="primary" 
              @click="$emit('viewDetail', scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="$emit('sizeChange', $event)"
        @current-change="$emit('currentChange', $event)"
      />
    </div>
  </div>
</template>

<script setup>
defineProps({
  bookings: {
    type: Array,
    default: () => []
  },
  total: {
    type: Number,
    default: 0
  },
  currentPage: {
    type: Number,
    default: 1
  },
  pageSize: {
    type: Number,
    default: 10
  },
  tableLoading: {
    type: Boolean,
    default: false
  },
  exportLoading: {
    type: Boolean,
    default: false
  }
})

defineEmits(['export', 'viewDetail', 'sizeChange', 'currentChange'])

// 格式化函数
const formatDate = (dateStr) => {
  if (!dateStr) return '未知'
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN')
}

const formatDateTime = (dateTimeStr) => {
  if (!dateTimeStr) return '未知'
  
  try {
    const date = new Date(dateTimeStr)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  } catch (error) {
    return '格式错误'
  }
}

const getStatusText = (status) => {
  const statusMap = {
    '已预约': '已预约',
    '已取消': '已取消',  
    '已完成': '已完成',
    'confirmed': '已确认',
    'completed': '已完成',
    'no_show': '未到场'
  }
  return statusMap[status] || status
}

const getStatusTagType = (status) => {
  const statusTypeMap = {
    '已预约': 'primary',
    '已取消': 'danger',
    '已完成': 'success',
    'confirmed': 'primary',
    'completed': 'success',
    'no_show': 'danger'
  }
  return statusTypeMap[status] || 'info'
}

const getPeriodTagType = (period) => {
  const periodTypeMap = {
    '上午': 'success',
    '下午': 'warning', 
    '晚上': 'info',
    '上午场次': 'success',
    '下午场次': 'warning',
    '晚上场次': 'info'
  }
  return periodTypeMap[period] || 'info'
}
</script>

<style scoped>
/* 预约数据表格样式 */
.table-panel {
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.table-header {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.table-count {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.table-actions {
  display: flex;
  gap: 10px;
}

.export-btn {
  padding: 12px 20px;
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  box-shadow: 0 4px 15px rgba(72, 187, 120, 0.2);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.export-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(72, 187, 120, 0.4);
}

.export-btn:active:not(:disabled) {
  transform: translateY(0);
}

.export-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.table-container {
  padding: 0;
}

.booking-number {
  font-family: 'Courier New', monospace;
  font-weight: bold;
  color: #667eea;
}

.contact-name {
  font-weight: 600;
  color: #333;
}

.contact-phone {
  font-family: 'Courier New', monospace;
  color: #666;
}

.personal-info {
  color: #333;
  font-size: 14px;
}

.booking-date {
  color: #333;
}

.booking-time {
  font-family: 'Courier New', monospace;
  color: #666;
}

.created-time {
  font-size: 12px;
  color: #888;
}

.notes {
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pagination-container {
  padding: 20px;
  display: flex;
  justify-content: center;
  border-top: 1px solid #f0f0f0;
}

.btn-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-icon {
  font-size: 1rem;
}

.btn-loading {
  display: flex;
  align-items: center;
  gap: 10px;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style> 